package com.example.manbook.controller;

import com.example.manbook.service.ResultService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/api")
@Slf4j
public class IndexController {

    @Autowired
    private ResultService resultService;



    @GetMapping("man")
    public List<String> listDirs() throws IOException, InterruptedException {
        String command = "ls -dl /usr/share/man/man*";
        log.info("command = {}", command);
        return resultService.execList(command);
    }

    @GetMapping("man/{dir}")
    public List<String> listCommands(@PathVariable String dir) throws IOException {
        String command = "ls -dl /usr/share/man/" + dir+"/*";
        log.info("command = {}", command);
        return resultService.execList(command);
    }

    @GetMapping("man/{dir}/{cmd}")
    public List<String> showInfo(@PathVariable String dir, @PathVariable String cmd) throws IOException {
        int num = Integer.parseInt(dir.substring(3));
        return resultService.execInfo(cmd, num);
    }
}
